package com.zsh.web;

import com.zsh.pojo.User;
import com.zsh.service.UserService;
import com.zsh.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2.判断用户名密码是否正确

        if(userService.login(new User(null,username,password,null)) == null){
            System.out.println("登录失败");
            //把错误信息和回显信息发送给jsp
            request.setAttribute("msg","用户名或密码错误！");
            request.setAttribute("username",username);

            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }else{
            System.out.println("登录成功");

            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }

    }

}
